<template>
  <div class="server-stats-system">
    <!-- 统计链接按钮 -->
    <div class="stats-link">
      <a href="/introduction/analytics" class="stats-btn" title="查看访问统计">
        📊 访问统计
      </a>
      <div class="connection-status" :class="{ online: isOnline, offline: !isOnline }">
        <span class="status-dot"></span>
        {{ isOnline ? '在线' : '离线' }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { serverAnalyticsService } from '../services/serverAnalytics'

const isOnline = ref(false)

// 更新连接状态
const updateConnectionStatus = async () => {
  try {
    isOnline.value = serverAnalyticsService.isServerOnline()
  } catch (error) {
    console.error('更新连接状态失败:', error)
    isOnline.value = false
  }
}

onMounted(async () => {
  // 更新连接状态
  await updateConnectionStatus()
  
  // 定期更新连接状态
  if (typeof window !== "undefined") {
    setInterval(updateConnectionStatus, 30000) // 每30秒更新一次
  }
})
</script>

<style scoped>
.server-stats-system {
  position: relative;
}

.stats-link {
  position: fixed;
  bottom: 20px;
  left: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 1000;
}

.stats-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  background: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 123, 255, 0.3);
  border: none;
  cursor: pointer;
}

.stats-btn:hover {
  background: #0056b3;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 123, 255, 0.4);
  color: white;
  text-decoration: none;
}

.stats-btn:active {
  transform: translateY(0);
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 15px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ccc;
  transition: background-color 0.3s;
}

.connection-status.online .status-dot {
  background: #28a745;
}

.connection-status.offline .status-dot {
  background: #dc3545;
}

@media (max-width: 768px) {
  .stats-link {
    bottom: 10px;
    left: 10px;
  }
  
  .stats-btn {
    padding: 10px 16px;
    font-size: 13px;
  }
}
</style> 